# -*- coding: utf-8 -*-
import time
from naoqi import ALProxy
import threading
#from playsound import playsound

def runBehaviors(managerProxy,dance_name):
    names = managerProxy.startBehavior(dance_name)
    print "runing!"
    print managerProxy.getRunningBehaviors()

#舞蹈名字
dance_name='guojia/behavior_1'
#dance_name='gangnam-style'
#dance_name='naner2/behavior_1'
#dance_name='thunder2/behavior_1'
#机器人ip地址列表
#IPs=['192.168.1.106','192.168.1.103','192.168.1.100','192.168.1.102','192.168.1.108']
IPs=['192.168.1.108']
managerProxyLists=[]
for i in range(len(IPs)):
    newManagerProxy=ALProxy("ALBehaviorManager", IPs[i], 9559)
    managerProxyLists.append(newManagerProxy)
    managerProxyLists[i].preloadBehavior(dance_name)

time.sleep(5)
threads = []
for i in range(len(IPs)):
    newThread = threading.Thread(target = runBehaviors, args = (managerProxyLists[i],dance_name))
    threads.append(newThread)
#如果需要连接音响播放音乐的话，则取消下面一行代码以及上面import的注释，
#并将对应的音频文件放在同一目录下，将自己电脑与音响连接即可，但是音响会慢几拍，看后续如何改进
#threads.append(threading.Thread(target = playsound, args = ('Guojia.mp3',)))
#开始各个线程，让各机器人开始跳舞
for t in threads:
    t.start()
#???好像是限制机器人只跳一定的时间
# time.sleep(180.0)
# if managerProxyLists[0].isBehaviorRunning(dance_name):
#     managerProxyLists[0].stopBehavior(dance_name)
#     time.sleep(1.0)
# else:
#     print "Behavior is already stopped."
